package com.cspm.ahjy.bc.office.word.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.dom4j.Attribute;
import org.dom4j.Element;
import org.springframework.util.StringUtils;

import com.cspm.ahjy.bc.office.enums.AlignEnum;
import com.cspm.ahjy.bc.office.enums.ColorEnum;
import com.cspm.ahjy.bc.office.util.UnitConvertUtil;
import com.cspm.ahjy.bc.office.word.domain.MSAction;
import com.cspm.ahjy.bc.office.word.domain.MSActionType;
import com.cspm.ahjy.bc.office.word.domain.MSFont;
import com.cspm.ahjy.bc.office.word.domain.MSItem;
import com.cspm.ahjy.bc.office.word.domain.MSParagraph;
import com.cspm.ahjy.bc.office.word.domain.MSPicture;
import com.cspm.ahjy.bc.office.word.domain.MSShadow;
import com.cspm.ahjy.bc.office.word.domain.MSWordart;

/**
 * 段落处理
 * 
 * @author ServerZhang
 * @date 2017年9月28日
 */
public class ParagraphUtil {

	/**
	 * 返回段落中的指定文字
	 * 
	 * @param paragraph
	 * @param text
	 *            指定文字，为空时表示获取全段文字
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月28日
	 */
	public static List<MSFont> getFonts(MSParagraph paragraph, String text) {
		List<MSFont> returnFonts = new ArrayList<MSFont>();

		if (null == paragraph) {
			return returnFonts;
		}

		List<MSFont> fonts = paragraph.getFonts();

		if (StringUtils.isEmpty(text)) {
			return fonts;
		}

		Iterator<MSFont> iterator = fonts.iterator();
		while (iterator.hasNext()) {
			MSFont next = iterator.next();
			String text2 = next.getText();
			if (text.equals(text2)) {
				returnFonts.add(next);
			}
		}
		return returnFonts;
	}

	/**
	 * 迭代获取<wx:sect>标签下的第一层<w:p>
	 * 
	 * @param element
	 *            传入的Element，一般认为是<wx:sect>标签
	 * @param paragraphs
	 *            已获取到的段落
	 * @param pCount
	 *            已获取到的段落数
	 * @param path 文档所在目录
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月27日
	 * @date 2017年10月11日 优化
	 */
	public static Map<String, List<MSParagraph>> getParagraphs(Element element,
			List<MSParagraph> inputParagraphs, Integer pCount, String path) {
		// 存储返回数据：key（页眉header，页脚footer，段落paragraph）
		Map<String, List<MSParagraph>> map = new HashMap<String, List<MSParagraph>>();

		List<MSParagraph> headers = new ArrayList<MSParagraph>();// 页眉
		List<MSParagraph> footers = new ArrayList<MSParagraph>();// 页脚
		List<MSParagraph> paragraphs = new ArrayList<MSParagraph>();// 真实段落
		List<MSParagraph> watermarks = new ArrayList<MSParagraph>();// 水印

		if (null == pCount || pCount.equals(0)) {
			pCount = inputParagraphs.size();
		}

		List<Element> elements = element.elements();
		Integer counter = elements.size();// 段落计数器,达到0时，表示获取到所有段落，返回List

		Map<Integer, Integer> firstWordSinkMap = null;// 暂存下一段落的首字下沉
		Map<Integer, MSFont> firstMsFontMap = null;// 暂存下一段落首字下沉的Font

		for (Element e : elements) {
			String name = e.getName();
			if ("pBdrGroup".equals(name)) {// 解析是否有首字下沉操作
				counter--;
				if (0 == counter) {
					map.put("header", headers);
					map.put("footer", footers);
					map.put("paragraph", paragraphs);
					map.put("watermark", watermarks);
					return map;
				}
				// 解析<wx:pBdrGroup>，如果存在，为下一个p标签的段落格式
				// 解析是否有首字下沉操作
				Element pE = e.element("p");
				if (pE != null) {
					Element pPr = pE.element("pPr");
					Element r = pE.element("r");
					if (pPr != null && r != null) {
						Element framePr = pPr.element("framePr");
						if (framePr != null) {
							Attribute lines = framePr.attribute("lines");
							if (lines != null) {
								if (firstWordSinkMap == null)
									firstWordSinkMap = new HashMap<Integer, Integer>();
								firstWordSinkMap.put(pCount,
										Integer.parseInt(lines.getText()));
							}
						}
						if (firstMsFontMap == null)
							firstMsFontMap = new HashMap<Integer, MSFont>();
						firstMsFontMap.put(pCount, FontUtil.getFont(r, pCount, path));
					}
				}
			} else if ("p".equals(name)) {// 是段落

				Map<String, List<MSParagraph>> pMap = getParagraph(e, pCount, path);

				List<MSParagraph> headerList = pMap.get("header");
				List<MSParagraph> footerList = pMap.get("footer");
				List<MSParagraph> paragraphList = pMap.get("paragraph");
				List<MSParagraph> watermarkList = pMap.get("watermark");

				if (headerList.size() > 0) {
					headers.addAll(headerList);
				}
				if (footerList.size() > 0) {
					footers.addAll(footerList);
				}
				if (watermarkList.size() > 0) {
					watermarks.addAll(watermarkList);
				}
				if (paragraphList.size() != 1) {
					continue;
				}

				MSParagraph paragraph = paragraphList.get(0);

				// 空段落
				if (null == paragraph) {
					continue;
				}
				// 解析出的上一段为此段的格式信息（首字下沉）
				if (firstWordSinkMap != null) {
					Integer line = firstWordSinkMap.get(pCount);
					if (line != null) {
						paragraph.setFirstWordSink(line);
						firstWordSinkMap = null;
					}
				}
				if (firstMsFontMap != null) {
					MSFont msFont = firstMsFontMap.get(pCount);
					String text = paragraph.getText();
					if (msFont != null) {
						if (text != null) {
							text = msFont.getText() + text;
						} else {
							text = msFont.getText();
						}
						paragraph.setText(text);
						firstMsFontMap = null;
					}
				}

				// 分栏信息存储段落
				String colsType = paragraph.getColsType();
				if (colsType != null) {
					if (paragraphs.size() > 0) {
						MSParagraph lastP = paragraphs
								.get(paragraphs.size() - 1);
						lastP.setColsType(colsType);
					}
					continue;
				}

				paragraphs.add(paragraph);
				pCount++;

				counter--;
				if (0 == counter) {
					map.put("header", headers);
					map.put("footer", footers);
					map.put("paragraph", paragraphs);
					map.put("watermark", watermarks);
					return map;
				}
			} else if ("sectPr".equals(name)) {// 此标签如果不存在页眉、页脚、水印则不再继续下挖

				// TODO 如果影响效率，这段删除，因为没有必要

				Element hdr = e.element("hdr");
				Element ftr = e.element("ftr");
				if (hdr == null && ftr == null) {
					counter--;
					if (0 == counter) {
						map.put("header", headers);
						map.put("footer", footers);
						map.put("paragraph", paragraphs);
						map.put("watermark", watermarks);
						return map;
					}
				}
				// 获取页眉、页脚、水印
				Map<String, List<MSParagraph>> allParagraph = getParagraphs(e,
						paragraphs, pCount, path);
				List<MSParagraph> headerList = allParagraph.get("header");
				List<MSParagraph> footerList = allParagraph.get("footer");
				List<MSParagraph> paragraphList = allParagraph.get("paragraph");
				List<MSParagraph> watermarkList = allParagraph.get("watermark");

				if (headerList.size() > 0) {
					headers.addAll(headerList);
				}
				if (footerList.size() > 0) {
					footers.addAll(footerList);
				}
				if (paragraphList.size() == 1) {
					paragraphs.addAll(paragraphList);
				}
				if (watermarkList.size() == 1) {
					watermarks.addAll(watermarkList);
				}

			} else {
				Map<String, List<MSParagraph>> allParagraph = getParagraphs(e,
						paragraphs, pCount, path);
				List<MSParagraph> headerList = allParagraph.get("header");
				List<MSParagraph> footerList = allParagraph.get("footer");
				List<MSParagraph> paragraphList = allParagraph.get("paragraph");
				List<MSParagraph> watermarkList = allParagraph.get("watermark");

				if (headerList.size() > 0) {
					headers.addAll(headerList);
				}
				if (footerList.size() > 0) {
					footers.addAll(footerList);
				}
				if (paragraphList.size() == 1) {
					paragraphs.addAll(paragraphList);
				}
				if (watermarkList.size() == 1) {
					watermarks.addAll(watermarkList);
				}
			}
		}
		map.put("header", headers);
		map.put("footer", footers);
		map.put("paragraph", paragraphs);
		map.put("watermark", watermarks);
		return map;
	}

	/**
	 * 获取所有段落 (包含页眉和页脚) 如果段落后紧随空段落，判断此空段落是否为分栏信息的储存位置
	 * 
	 * @param element
	 * @param path 文档所在目录 
	 * @return
	 * @author ServerZhang
	 * @date 2017年9月27日
	 */
	@Deprecated
	public static Map<String, List<MSParagraph>> getAllParagraph(Element element, String path) {
		// 存储返回数据：key（页眉header，页脚footer，段落paragraph）
		Map<String, List<MSParagraph>> map = new HashMap<String, List<MSParagraph>>();

		List<MSParagraph> headers = new ArrayList<MSParagraph>();// 页眉
		List<MSParagraph> footers = new ArrayList<MSParagraph>();// 页脚
		List<MSParagraph> paragraphs = new ArrayList<MSParagraph>();// 真实段落

		//List<Element> selectNodes = element.selectNodes("//w:p");
		List<Element> selectNodes = new ArrayList<Element>();
		int size = selectNodes.size();

		int pCount = 0;
		for (int i = 0; i < size; i++) {
			Element pElement = selectNodes.get(i);

			// TODO 如果用到此方法再完善

			Map<String, List<MSParagraph>> pMap = getParagraph(pElement, pCount, path);

			List<MSParagraph> list = pMap.get("paragraph");
			if (list.size() != 1) {
				continue;
			}

			MSParagraph paragraph = pMap.get("paragraph").get(0);

			// 空段落
			if (paragraph == null) {
				continue;
			}
			// 分栏信息存储段落
			String colsType = paragraph.getColsType();
			if (colsType != null) {
				if (paragraphs.size() > 0) {
					MSParagraph lastP = paragraphs.get(paragraphs.size() - 1);
					lastP.setColsType(colsType);
				}
				continue;
			}
			// 页眉

			// 页脚

			pCount++;
			paragraphs.add(paragraph);
		}

		map.put("header", headers);
		map.put("footer", footers);
		map.put("paragraph", paragraphs);

		return map;
	}

	/**
	 * 比较两个段落
	 * 
	 * @param msParagraph1
	 * @param msParagraph2
	 * @author ServerZhang
	 * @date 2017年10月9日
	 */
	public static List<MSAction> compare(MSParagraph msParagraph1,
			MSParagraph msParagraph2, Integer paragraphNo) {
		List<MSAction> actions = new ArrayList<MSAction>();

		/*
		 * if (paragraphNo.equals(0)) {
		 * 
		 * }
		 */

		// 1.段落属性比较
		List<MSAction> comparePro = compareProperty(msParagraph1, msParagraph2,
				paragraphNo);
		if (comparePro != null && comparePro.size() > 0) {
			actions.addAll(comparePro);
		}
		// 2.判断是整段操作还是部分文字操作
		MSFont wholeFont1 = msParagraph1.getWholeFont();
		MSFont wholeFont2 = msParagraph2.getWholeFont();
		
		if (wholeFont2 != null) {// 整段操作

			MSWordart msWordart2 = wholeFont2.getMsWordart();
			MSPicture msPicture2 = wholeFont2.getMsPicture();

			if (msWordart2 != null) {// 整段为艺术字
				if (wholeFont1 != null) {// 原始文档中的段落为统一整体
					MSWordart msWordart1 = wholeFont1.getMsWordart();
					if (msWordart1 != null) {
						List<MSAction> compare = WordartUtil.compare(msWordart1, msWordart2,paragraphNo,Boolean.TRUE);
						if (compare != null && compare.size() > 0) {
							actions.addAll(compare);
						}
					} else {
						List<MSAction> compare = WordartUtil.compare(
								wholeFont1, msWordart2, paragraphNo,
								Boolean.TRUE);
						if (compare != null && compare.size() > 0) {
							actions.addAll(compare);
						}
					}
				} else {
					List<MSAction> addWordart = WordartUtil.addWordart(
							msWordart2, paragraphNo, Boolean.TRUE);
					if (addWordart != null && addWordart.size() > 0) {
						actions.addAll(addWordart);
					}
				}
			} else if (msPicture2 != null) {// 一张图片
				if (wholeFont1 != null) {// 原始文档中的段落为统一整体
					MSPicture msPicture1 = wholeFont1.getMsPicture();
					List<MSAction> comparePicture = PictureUtil.comparePicture(
							msPicture1, msPicture2, paragraphNo);
					if (comparePicture != null && comparePicture.size() > 0) {
						actions.addAll(comparePicture);
					}
				} else {
					List<MSAction> addPicture = PictureUtil.addPicture(
							msPicture2, paragraphNo);
					if (addPicture != null && addPicture.size() > 0) {
						actions.addAll(addPicture);
					}
				}

			} else {// 普通文本
				if (wholeFont1 != null) {// 原始文档中的段落为统一整体
					List<MSAction> compare = FontUtil.compare(wholeFont1,
							wholeFont2, paragraphNo, true);
					if (compare != null && compare.size() > 0) {
						actions.addAll(compare);
					}
				} else {
					List<MSAction> compare = FontUtil.addFont(wholeFont2,
							paragraphNo, true);
					if (compare != null && compare.size() > 0) {
						actions.addAll(compare);
					}
				}
			}

			return actions;
		}

		// 3.段落中的文字比较
		List<MSFont> fonts1 = msParagraph1.getFonts();
		List<MSFont> fonts2 = msParagraph2.getFonts();

		int fCount1 = fonts1.size();
		int fCount2 = fonts2.size();// 理论上fCount2>=fCount1

		int nonius = 0;// font1的游标
		String tempText = "";
		for (int j = 0; j < fCount2; j++) {// 遍历标准答案
			MSFont msFont2 = fonts2.get(j);

			Integer type2 = msFont2.getType();
			String text2 = msFont2.getText();

			for (int k = nonius; k < fCount1; k++) {// 遍历初始文档
				MSFont msFont1 = fonts1.get(k);
				Integer type1 = msFont1.getType();
				String text1 = msFont1.getText();

				if (type1 != type2) {// 字体类型不同
					if (type2.equals(0)) {// 理论上不存在此情况
						if (k == fCount1) {// 遍历初始文档到段尾，说明标准答案中的此字体为新增的
							FontUtil.addFont(msFont2, paragraphNo, null);
						}
					} else if (type2.equals(1)) {
						MSWordart msWordart = msFont2.getMsWordart();
						String msText = msWordart.getText();
						if (text1.equals(msText)) {
							List<MSAction> compare = WordartUtil.compare(
									msFont1, msWordart, paragraphNo, null);
							if (compare.size() > 0) {
								actions.addAll(compare);
							}
							nonius++;
						} else if (text1.contains(msText)) {
							List<MSAction> compare = WordartUtil.compare(
									msFont1, msWordart, paragraphNo, null);
							if (compare.size() > 0) {
								actions.addAll(compare);
							}
						} else {// 在tempText后新增了艺术字
							System.out.print("在" + tempText + "之后添加艺术字：");
							List<MSAction> addWordart = WordartUtil.addWordart(
									msWordart, paragraphNo, null);
							if (addWordart.size() > 0) {
								actions.addAll(addWordart);
							}
						}
						tempText = "艺术字“" + msText + "”";
						break;
					} else if (type2.equals(2)) {//图片
						if (k == fCount1-1) {// 遍历初始文档到段尾，说明标准答案中的此字体为新增的
							List<MSAction> addPicture = PictureUtil.addPicture(
									msFont2.getMsPicture(), paragraphNo);
							if (addPicture != null && addPicture.size() > 0) {
								actions.addAll(addPicture);
							}
						}
					}
				} else {// 类型一致
					if (type2 == 0) {
						if (text1.equals(text2)) {
							List<MSAction> fontCompare = FontUtil.compare(
									msFont1, msFont2, paragraphNo, null);
							if (fontCompare != null && fontCompare.size() > 0) {
								actions.addAll(fontCompare);
							}
							nonius++;
						} else if (text1.contains(text2)) {
							List<MSAction> fontCompare = FontUtil.compare(
									msFont1, msFont2, paragraphNo, null);
							if (fontCompare != null && fontCompare.size() > 0) {
								actions.addAll(fontCompare);
							}
						} else if (text2.contains(text1)) {// 原则上应该避免此种情况

						} else {// 1.是部分包含情况,原则上应该避免此种情况；2.font2是新添加的
								// System.out.print("在"+tempText+"之后添加文字：");
								// FontUtil.addFont(msFont2);
						}
						tempText = "文字“" + text2 + "”";
						break;
					} else if (1 == type2) {
						MSWordart msWordart1 = msFont1.getMsWordart();
						MSWordart msWordart2 = msFont2.getMsWordart();
						List<MSAction> compare = WordartUtil.compare(msWordart1, msWordart2,paragraphNo,Boolean.TRUE);
						if (compare != null && compare.size() > 0) {
							actions.addAll(compare);
						}
						//String msText1 = msWordart1.getText();
						String msText2 = msWordart2.getText();

						tempText = "艺术字“" + msText2 + "”";
					} else if (2 == type2) {
						System.out.print("将" + tempText + "后的图片修改为：");
						MSPicture msPicture1 = msFont1.getMsPicture();
						MSPicture msPicture2 = msFont2.getMsPicture();
						String title1 = msPicture1.getTitle();
						String title2 = msPicture2.getTitle();
						if (!StringUtils.isEmpty(title1)
								&& title1.equals(title2)) {
							List<MSAction> comparePicture = PictureUtil
									.comparePicture(msPicture1, msPicture2,
											paragraphNo);
							if (comparePicture != null
									&& comparePicture.size() > 0) {
								actions.addAll(comparePicture);
							}
						}

						tempText = "图片“" + title2 + "”";
					}
				}
			}
		}
		return actions;
	}

	/**
	 * 解析<w:p>标签，获取文档MSParagraph对象 对于段落中的文字有两种情况：整段文字处理；部分文字处理。
	 * 如果是对整段文字进行处理，将不再解析“r”标签
	 * 
	 * @param pElement
	 * @param path 文档所在目录
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月9日
	 */
	public static Map<String, List<MSParagraph>> getParagraph(Element pElement,
			Integer pCount, String path) {
		Map<String, List<MSParagraph>> map = new HashMap<String, List<MSParagraph>>();

		List<MSParagraph> headers = new ArrayList<MSParagraph>();// 页眉，可能存在多个
		List<MSParagraph> footers = new ArrayList<MSParagraph>();// 页脚，可能存在多个
		List<MSParagraph> paragraphs = new ArrayList<MSParagraph>();// 真实段落,如果有，仅有一个
		List<MSParagraph> watermarks = new ArrayList<MSParagraph>();// 水印,如果有，仅有一个

		MSParagraph msParagraph = new MSParagraph();

		// 1.获取段落中的文字，进行空段落判断
		List<Element> runs = pElement.elements("r");
		// 2.获取段落样式
		Element pPr = pElement.element("pPr");

		if (runs.size() <= 0) {// 空段落或分栏信息
			// 判断是否是为上一段落的分栏信息
			if (pPr != null) {
				Element sectPr = pPr.element("sectPr");
				if (sectPr != null) {
					Element cols = sectPr.element("cols");
					if (cols != null) {// 说明是上一段落的分栏信息
						Attribute num = cols.attribute("num");
						if (num != null) {
							int parseInt = Integer.parseInt(num.getText());
							switch (parseInt) {
							case 1:
								msParagraph.setColsType("一栏");
								break;
							case 2:
								List<Element> col = cols.elements("col");
								if (col.size() == 2) {// 如果没有则说明是分成平均两栏
									Element col1 = col.get(0);
									Element col2 = col.get(1);
									Attribute w1 = col1.attribute("w");
									Attribute w2 = col2.attribute("w");
									if (w1 != null && w2 != null) {
										int width1 = Integer.parseInt(w1
												.getText());
										int width2 = Integer.parseInt(w2
												.getText());
										if (width1 == width2) {
											msParagraph.setColsType("两栏");
										} else if (width1 > width2) {
											msParagraph.setColsType("“偏右”的两栏");
										} else {
											msParagraph.setColsType("“偏左”的两栏");
										}
									}
								} else {
									msParagraph.setColsType("两栏");
								}
								break;
							case 3:
								msParagraph.setColsType("三栏");
								break;

							default:
								msParagraph.setColsType(parseInt + "栏");
								break;
							}
							paragraphs.add(msParagraph);
						}
					} else {// 可能是页眉或页脚或水印

						// Element element = sectPr.element("hdr");

						// 由于标签的<w:hdr>和<w:ftr>位置的极度不确定性，所以段落标签下去解析，使用路径表达式单独处理
					}
				}
			}
			map.put("header", headers);
			map.put("footer", footers);
			map.put("paragraph", paragraphs);
			map.put("watermark", watermarks);

			return map;
		}

		if (pPr == null) {// 理论上不可能没有段落样式
			paragraphs.add(msParagraph);
			map.put("header", headers);
			map.put("footer", footers);
			map.put("paragraph", paragraphs);
			map.put("watermark", watermarks);

			return map;
		}

		msParagraph.setParagraphNum(pCount);
		// 3.判断是整段文字处理还是部分文字处理
		if (runs.size() == 1) {// 整段处理
			Element element = runs.get(0);
			MSFont msFont = FontUtil.getFont(element, pCount, path);
			msParagraph.addFonts(msFont);
			// MSFont wholeFont = FontUtil.getFont(pPr, pCount);
			msParagraph.setWholeFont(msFont);

			msParagraph.setText(msFont.getText());
		} else {
			Iterator<Element> iterator = runs.iterator();
			
			String fontNameTemp = null;// 字体
			Integer fontSizeTemp = null;// 字号
			String colorTemp = null;// 颜色，默认黑色
			/*Boolean boldTemp = null;// 加粗
			Boolean italicTemp = null;// 斜体
			String underlineTemp = null;// 下划线样式
			String underlineColorTemp = null;// 下划线颜色，默认黑色
			Boolean strikeTemp = null;// 删除线
			Boolean dbStrikeTemp = null;// 双删除线
			String stressTemp = null;// 着重号样式
*/			
			int index = 0;
			while (iterator.hasNext()) {
				Element next = iterator.next();

				MSFont msFont = FontUtil.getFont(next, pCount, path);

				msParagraph.addFonts(msFont);
				
				String fontText = msFont.getText();
				if (StringUtils.isEmpty(fontText)) {
					continue;
				}
				String text = msParagraph.getText();
				if (StringUtils.isEmpty(text)) {
					text = fontText;
				} else {
					text += fontText;
				}
				msParagraph.setText(text);
				
				//获取文字属性，如果全部相同则赋值到段落
				String fontName = msFont.getFontName();
				if (0 == index) {
					fontNameTemp = fontName;
				}else{
					if(fontNameTemp == null){
					} else if(fontNameTemp != fontName){
						fontNameTemp = null;
					}
				}
				Integer fontSize = msFont.getFontSize();
				if (0 == index) {
					fontSizeTemp = fontSize;
				}else{
					if(fontSizeTemp == null){
					} else if(fontSizeTemp != fontSize){
						fontSizeTemp = null;
					}
				}
				String color = msFont.getColor();
				if (0 == index) {
					colorTemp = color;
				}else{
					if(colorTemp == null){
					} else if(colorTemp != color){
						colorTemp = null;
					}
				}
				
				
				index ++;
			}
			
			MSFont msFont = null;
			if (fontNameTemp != null) {
				if (null == msFont) {
					msFont = new MSFont();
				}
				msFont.setFontName(fontNameTemp);
			}
			if (fontSizeTemp != null) {
				if (null == msFont) {
					msFont = new MSFont();
				}
				msFont.setFontSize(fontSizeTemp);
			}
			if (colorTemp != null) {
				if (null == msFont) {
					msFont = new MSFont();
				}
				msFont.setColor(colorTemp);
			}
			if (msFont != null) {
				msParagraph.setWholeFont(msFont);
			}
			
		}

		// 4.段落样式解析
		// 项目编号或符号
		Element listE = pPr.element("listPr");
		if (listE != null) {
			Integer ilfo = null;
			String text = null;
			Element ilfoE = listE.element("ilfo");
			if (ilfoE != null) {
				Attribute val = ilfoE.attribute("val");
				if (val != null) {
					ilfo = Integer.parseInt(val.getText());
				}
			}
			Element tE = listE.element("t");
			if (tE != null) {
				Attribute val = tE.attribute("val");
				if (val != null) {
					text = val.getText();
				}
			}
			MSItem msItem = new MSItem(ilfo, text);
			Element fontE = listE.element("font");
			if (fontE != null) {
				Attribute val = fontE.attribute("val");
				if (val != null) {
					msItem.setFont(val.getText());
				}
			}
			msParagraph.setMsItem(msItem);
		}
		// 对齐方式
		Element jcElement = pPr.element("jc");
		if (jcElement != null) {
			Attribute val = jcElement.attribute("val");
			if (val != null) {
				msParagraph.setAlignment(val.getText());
			}
		} else {
			msParagraph.setAlignment("double");// 两端对齐
		}
		// 间距
		Element spacing = pPr.element("spacing");
		if (spacing != null) {
			Attribute before = spacing.attribute("before-lines");// 段前
			if (before != null) {
				msParagraph.setBeforeLines(Integer.parseInt(before.getText()));
			}
			Attribute after = spacing.attribute("after-lines");// 段后
			if (after != null) {
				msParagraph.setAfterLines(Integer.parseInt(after.getText()));
			}
			Attribute lineRule = spacing.attribute("line-rule");// 行距
			Attribute line = spacing.attribute("line");// 行距值
			if (lineRule != null && line != null) {
				String rule = lineRule.getText();
				Integer lineValue = Integer.parseInt(line.getText());

				msParagraph.setLineRule(rule);
				msParagraph.setLine(lineValue);
				switch (rule) {
				case "auto":
					msParagraph.setLineDescription(UnitConvertUtil
							.standar2RowTimes(lineValue));
					break;
				case "at-least":
					String description = "最小值（设置值为 "
							+ UnitConvertUtil.standar2PT(lineValue) + " ）";
					msParagraph.setLineDescription(description);
					break;
				case "exact":
					msParagraph.setLineDescription("固定值（设置值为 "
							+ UnitConvertUtil.standar2PT(lineValue) + " ）");
					break;

				default:
					break;
				}
			} else {// 单倍行距
				msParagraph.setLineRule("auto");
				msParagraph.setLine(240);
				msParagraph.setLineDescription("单倍行距");
			}
		}
		// 缩进方式及值（仅限字符）
		Element indE = pPr.element("ind");
		if (indE != null) {
			// 左缩进（字符）
			Attribute left = indE.attribute("left-chars");
			if (left != null) {
				msParagraph
						.setIndentationLeft(Integer.parseInt(left.getText()));
			}
			// 右缩进（字符）
			Attribute right = indE.attribute("right-chars");
			if (right != null) {
				msParagraph.setIndentationRight(Integer.parseInt(right
						.getText()));
			}
			// 首行缩进（字符）
			Attribute first = indE.attribute("first-line-chars");
			if (first != null) {
				msParagraph.setIndentationFirstline(Integer.parseInt(first
						.getText()));
			}
			// 悬挂缩进（字符）
			Attribute hanging = indE.attribute("hanging-chars");
			if (hanging != null) {
				msParagraph.setIndentationHanging(Integer.parseInt(hanging
						.getText()));
			}
		}
		// 底纹
		Element shdE = pPr.element("shd");
		if (shdE != null) {
			MSShadow msShadow = ShadowUtil.getMsShadow(shdE);
			msParagraph.setMsShadow(msShadow);
		}
		// 首字下沉，在BodyUtil中处理

		paragraphs.add(msParagraph);
		map.put("header", headers);
		map.put("footer", footers);
		map.put("paragraph", paragraphs);
		map.put("watermark", watermarks);

		return map;
	}

	public static List<MSAction> compareProperty(MSParagraph msParagraph1,
			MSParagraph msParagraph2, Integer paragraphNo) {
		List<MSAction> actions = new ArrayList<MSAction>();

		Integer[] paragraphNos = new Integer[1];
		if (paragraphNo != null) {
			paragraphNos[0] = paragraphNo;
		}

		// 项目符号/编号
		MSItem msItem1 = msParagraph1.getMsItem();
		MSItem msItem2 = msParagraph2.getMsItem();
		if (msItem2 != null && !msItem2.equals(msItem1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_msItem");
			actionType.setTypeValue(msItem2);
			action.setParagraphNos(paragraphNos);
			String content = "添加项目"
					+ ("number".equals(msItem2.getItemType()) ? "编号" : "符号")
					+ "，样式为" + msItem2.getDescription();
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 对齐方式
		String alignment1 = msParagraph1.getAlignment();
		String alignment2 = msParagraph2.getAlignment();
		if (alignment2 != null && !alignment2.equals(alignment1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_alignment");
			actionType.setTypeValue(alignment2);
			action.setParagraphNos(paragraphNos);

			String content = "对齐方式为“";
			AlignEnum byValue = AlignEnum.getByValue(alignment2);
			if (byValue != null) {
				content += byValue.getText();
			}else {
				content += alignment2;
			}
			content += "”";
			
			/*switch (alignment2) {
			case "left":
				content += "左对齐";
				break;
			case "center":
				content += "居中对齐";
				break;
			case "right":
				content += "右对齐";
				break;
			case "double":
				content += "两端对齐";
				break;
			case "distribute":
				content += "分散对齐";
				break;

			default:
				break;
			}*/

			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 段前、段后、行距
		Integer beforeLines1 = msParagraph1.getBeforeLines();
		Integer beforeLines2 = msParagraph2.getBeforeLines();
		if (beforeLines2 != null && !beforeLines2.equals(beforeLines1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_beforeLines");
			actionType.setTypeValue(beforeLines2);
			action.setParagraphNos(paragraphNos);
			String content = "段前间距 "
					+ UnitConvertUtil.standar2Row(beforeLines2);
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Integer afterLines1 = msParagraph1.getAfterLines();
		Integer afterLines2 = msParagraph2.getAfterLines();
		if (afterLines2 != null && !afterLines2.equals(afterLines1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_beforeLines");
			actionType.setTypeValue(afterLines2);
			action.setParagraphNos(paragraphNos);
			String content = "段后间距 " + UnitConvertUtil.standar2Row(afterLines2);
			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		String lineDescription1 = msParagraph1.getLineDescription();
		String lineDescription2 = msParagraph2.getLineDescription();
		if (lineDescription2 != null
				&& !lineDescription2.equals(lineDescription1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_line");
			actionType.setTypeValue(lineDescription2);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("行距为 " + lineDescription2);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 缩进方式及值（仅限字符）
		// 左缩进
		Integer indentationLeft1 = msParagraph1.getIndentationLeft();
		Integer indentationLeft2 = msParagraph2.getIndentationLeft();
		if (indentationLeft2 != null
				&& !indentationLeft2.equals(indentationLeft1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_indentationLeft");
			actionType.setTypeValue(indentationLeft2);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("左缩进 "
					+ UnitConvertUtil.standar2Char(indentationLeft2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 右缩进
		Integer indentationRight1 = msParagraph1.getIndentationRight();
		Integer indentationRight2 = msParagraph2.getIndentationRight();
		if (indentationRight2 != null
				&& !indentationRight2.equals(indentationRight1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_indentationRight");
			actionType.setTypeValue(indentationRight2);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("右缩进 "
					+ UnitConvertUtil.standar2Char(indentationRight2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 首行缩进和悬挂缩进（二选一）
		Integer indentationFirstline1 = msParagraph1.getIndentationFirstline();
		Integer indentationFirstline2 = msParagraph2.getIndentationFirstline();
		if (indentationFirstline2 != null && !indentationFirstline2.equals(0)
				&& !indentationFirstline2.equals(indentationFirstline1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_indentationFirstline");
			actionType.setTypeValue(indentationFirstline2);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("首行缩进 "
					+ UnitConvertUtil.standar2Char(indentationFirstline2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		Integer indentationHanging1 = msParagraph1.getIndentationHanging();
		Integer indentationHanging2 = msParagraph2.getIndentationHanging();
		if (indentationHanging2 != null && !indentationHanging2.equals(0)
				&& !indentationHanging2.equals(indentationHanging1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_indentationHanging");
			actionType.setTypeValue(indentationHanging2);
			action.setParagraphNos(paragraphNos);
			actionType.setTypeContent("悬挂缩进 "
					+ UnitConvertUtil.standar2Char(indentationHanging2));
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 底纹
		MSShadow msShadow1 = msParagraph1.getMsShadow();
		MSShadow msShadow2 = msParagraph2.getMsShadow();
		if (msShadow2 != null && !msShadow2.equals(msShadow1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_msShadow");
			actionType.setTypeValue(msShadow2);
			action.setParagraphNos(paragraphNos);

			//String type = msShadow2.getType();
			String typeDescription = msShadow2.getTypeDescription();
			String color = msShadow2.getColor();
			String fill = msShadow2.getFill();
			String content = "底纹样式为“" + typeDescription + "”，颜色为“";
			
			ColorEnum byValue = ColorEnum.getByValue(color);
			if (byValue != null) {
				content += byValue.getName();
			}else {
				content += color;
			}
			content += "”，填充色为“";
			
			ColorEnum fillValue = ColorEnum.getByValue(fill);
			if (fillValue != null) {
				content += fillValue.getName();
			}else {
				content += fill;
			}
			content += "”";
			

			actionType.setTypeContent(content);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 分栏
		String colsType1 = msParagraph1.getColsType();
		String colsType2 = msParagraph2.getColsType();
		if (colsType2 != null && !colsType2.equals(colsType1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_colsType");
			actionType.setTypeValue(colsType2);
			action.setParagraphNos(paragraphNos);

			actionType.setTypeContent("分成" + colsType2);
			action.setMsActionType(actionType);
			actions.add(action);
		}
		// 首字下沉
		Integer firstWordSink1 = msParagraph1.getFirstWordSink();
		Integer firstWordSink2 = msParagraph2.getFirstWordSink();
		if (firstWordSink2 != null && !firstWordSink2.equals(firstWordSink1)) {
			MSAction action = new MSAction();
			MSActionType actionType = new MSActionType();
			actionType.setType("paragraph_firstWordSink");
			actionType.setTypeValue(firstWordSink2);
			action.setParagraphNos(paragraphNos);

			actionType.setTypeContent("首字下沉 " + firstWordSink2 + " 行");
			action.setMsActionType(actionType);
			actions.add(action);

		}
		return actions;
	}

	/**
	 * 新增段落
	 * 
	 * @param paragraph
	 * @param paragraphNo
	 * @return
	 * @author ServerZhang
	 * @date 2017年10月12日
	 */
	public static List<MSAction> addParagraph(MSParagraph paragraph,
			Integer paragraphNo) {
		List<MSAction> actions = new ArrayList<MSAction>();
		MSFont wholeFont = paragraph.getWholeFont();
		if (wholeFont != null) {
			MSPicture msPicture = wholeFont.getMsPicture();
			MSWordart msWordart = wholeFont.getMsWordart();

			List<MSAction> addInfor = new ArrayList<MSAction>();
			if (msPicture != null) {
				addInfor = PictureUtil.addPicture(msPicture, paragraphNo);
			} else if (msWordart != null) {
				addInfor = WordartUtil.addWordart(msWordart, paragraphNo,
						Boolean.TRUE);
			} else {
				addInfor = FontUtil.addFont(wholeFont, paragraphNo,
						Boolean.TRUE);
			}
			if (addInfor.size() > 0) {
				actions.addAll(addInfor);
			}
		} else {// TODO 插入一段格式不一致的内容，待完成

		}

		return actions;
	}
}
